package com.iweb.service;

import com.common.datasource.DataSourceUtil;
import com.iweb.dao.GoodDao;
import com.iweb.dao.GoodDaoImpl;
import com.iweb.dao.PicDao;
import com.iweb.dao.PicDaoImpl;
import com.iweb.pojo.Admin;
import com.iweb.pojo.Good;
import com.iweb.pojo.Pic;

import java.util.ArrayList;
import java.util.List;

/*
 *作者：lth
 *时间：2020/8/11 17:00
 *描述：
 */
public class GoodService {
    public List<Good> selectGoodList(String title) {

        List<Good> goods = new ArrayList<>();
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        GoodDao goodDao = new GoodDaoImpl(dataSourceUtil.getConnection());
        PicDao picDao = new PicDaoImpl(dataSourceUtil.getConnection());
        try {
            // 查找商品
            goods = goodDao.selectList(title);
            // 补全数据  图片的数据
            for (int i = 0; i < goods.size(); i++) {
                Good good = goods.get(i);
                List<Pic> pics = picDao.selectByGid(good.getGid());
                for (int j = 0; j < pics.size(); j++) {
                    Pic pic = pics.get(j);
                    // 显示的图片
                    if (pic.getActive().equals("0")) {
                        good.setActivePath(pic.getPath());
                    }
                    // 所有图片
                    good.getListPath().add(pic.getPath());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dataSourceUtil.close();
        }
        return goods;
    }

    public List<Good> selectOrderList(String aid) {

        List<String> goods = new ArrayList<>();

        Admin admin = null;
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        GoodDao goodDao = new GoodDaoImpl(dataSourceUtil.getConnection());
        PicDao picDao = new PicDaoImpl(dataSourceUtil.getConnection());

        List<Good> goodsList = new ArrayList<>();
        //保存一串good
        List<Good> goodsOrders = new ArrayList<>();
        try {
            // 查找商品
            goodsList = picDao.selectByUid(aid);
            //goodsList保存了一串good

            // 找出每一个商品的主要图片
            for (int i = 0; i < goodsList.size(); i++) {

                Good good = goodsList.get(i);
                Good good1 = new Good();
                List<Pic> pics = picDao.selectByGid(good.getGid());
                //找出此gid的其他属性
                good1 = goodDao.selectListbyGid(good.getGid());
                //整合两个good到一个里面
                good.setTitle(good1.getTitle());
                good.setPrice(good1.getPrice());
                for (int j = 0; j < pics.size(); j++) {
                    Pic pic = pics.get(j);
                    // 显示的图片
                    if (pic.getActive().equals("0")) {
                        good.setActivePath(pic.getPath());
                    }
                }
                goodsOrders.add(good);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dataSourceUtil.close();
        }

        return goodsOrders;
    }

    /*根据gid查看商品详情*/
    public Good selectGoodList2(String gid) {
        Good good = new Good();
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        GoodDao goodDao = new GoodDaoImpl(dataSourceUtil.getConnection());
        PicDao picDao = new PicDaoImpl(dataSourceUtil.getConnection());
        try {
            // 查找商品
            good = goodDao.selectListbyGid(gid);
            // 补全数据  图片的数据
            List<Pic> pics = picDao.selectByGid(good.getGid());
            for (int j = 0; j < pics.size(); j++) {
                Pic pic = pics.get(j);
                // 显示的图片
                if (pic.getActive().equals("0")) {
                    good.setActivePath(pic.getPath());
                }
                // 所有图片
                good.getListPath().add(pic.getPath());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dataSourceUtil.close();
        }

        return good;
    }
}


